package com.hzcx.app.simplechat.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.login.bean.LoginInfoBean;
import com.hzcx.app.simplechat.ui.login.contract.BindPhoneContract;
import com.hzcx.app.simplechat.ui.login.event.PhoneCityEvent;
import com.hzcx.app.simplechat.ui.login.presenter.BindPhonePresenter;
import com.hzcx.app.simplechat.ui.login.util.LoginUtil;
import com.hzcx.app.simplechat.ui.publicui.WebViewActivity;
import com.hzcx.app.simplechat.ui.publicui.bean.ProtocolBean;
import com.hzcx.app.simplechat.ui.publicui.enums.SendCodeEnum;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.view.MainBlueConfirmButton;
import com.hzcx.app.simplechat.view.SendCodeTextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定手机号页面
 */
public class BindPhoneActivity extends BaseActivity<BindPhonePresenter> implements BindPhoneContract.View {

    public static final String INTENT_LOGIN_INFO = "INTENT_LOGIN_INFO";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_phone_city)
    TextView tvPhoneCity;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.tv_send_code)
    SendCodeTextView tvSendCode;
    @BindView(R.id.ll_code)
    LinearLayout llCode;
    @BindView(R.id.btn_commit)
    MainBlueConfirmButton btnCommit;
    @BindView(R.id.tv_tips)
    TextView tvTips;

    private LoginInfoBean loginInfoBean;

    @Override
    public int getLayout() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public void initPresenter() {
        mPresenter=new BindPhonePresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("验证手机");

        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                checkData();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        etCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                checkData();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {
        loginInfoBean = (LoginInfoBean) getIntent().getSerializableExtra(INTENT_LOGIN_INFO);
    }

    @OnClick({R.id.tv_phone_city, R.id.tv_send_code, R.id.btn_commit,R.id.tv_tips})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_phone_city:
                startActivity(new Intent(this, PhoneCityActivity.class));
                break;

            case R.id.tv_send_code: // 发送验证码
                if (EmptyUtils.isEmpty(etPhone.getText().toString())) {
                    showError("请输入手机号码");
                    return;
                }
                mPresenter.sendCode(this, etPhone.getText().toString(), SendCodeEnum.BIND_PHONE,
                        tvPhoneCity.getText().toString());
                break;

            case R.id.btn_commit: // 提交
                mPresenter.bindPhone(this, loginInfoBean.getToken(), etPhone.getText().toString(), etCode.getText().toString(),
                        tvPhoneCity.getText().toString());
                break;

            case R.id.tv_tips:
                mPresenter.getProtocol(this,"ver_code");
                break;
        }
    }

    /**
     * 判断手机号及验证码是否输入完成
     */
    private void checkData() {
        if (EmptyUtils.isNotEmpty(etPhone.getText().toString())
                && EmptyUtils.isNotEmpty(etCode.getText().toString())) {
            btnCommit.setBtnEnabled(true);
        }
    }

    /**
     * 选择手机号地区返回
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void phoneCityChange(PhoneCityEvent event) {
        tvPhoneCity.setText(event.getBean().getPrefix());
    }

    /**
     * 验证码发送成功
     */
    @Override
    public void sendCodeSuccess() {
        tvSendCode.start();
    }

    /**
     * 手机号绑定成功
     */
    @Override
    public void bindSuccess() {
        showError("绑定成功");
        loginInfoBean.setMobile(etPhone.getText().toString());
        LoginUtil.loginSuccess(this, loginInfoBean);
    }

    /**
     * 收不到验证码地址获取成功
     *
     * @param data
     */
    @Override
    public void protocolResult(ProtocolBean data) {
        startActivity(new Intent(this, WebViewActivity.class)
                .putExtra(WebViewActivity.INTENT_TITLE, data.getName())
                .putExtra(WebViewActivity.INTENT_WEB_URL, data.getContenturl()));
    }
}
